import { request } from 'umi';
import type { ResponseData } from '@/utils/typings.d';
import { SUCCESS_CODE } from '@/utils/typings.d';
import type {
  AnyIotSimDetail,
  CreateIotSimType,
  DiscardedIotSimTableListItem,
  IotSimTableListItem,
  MultiSimOperInfo,
  UpdateIotSimType,
} from './typings.d';

export async function queryIotSimList(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: IotSimTableListItem[];
}> {
  const res = await request('/api/iot-sim/v1.0.0/list', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function createIotSim(params: CreateIotSimType) {
  const res = await request('/api/iot-sim/v1.0.0/iotSim', {
    method: 'POST',
    data: params,
  });
  if (res?.code === SUCCESS_CODE) return { data: res.data, success: true, message: '添加成功' };
  return { data: res?.data, success: false, message: res?.message || '添加失败' };
}

export async function removeIotSim(id: string) {
  const res = await request(`/api/iot-sim/v1.0.0/iotSim/${id}`, {
    method: 'DELETE',
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '删除成功' };
  return { success: false, message: res?.message || '删除失败' };
}

export async function updateIotSim(id: string, newInfo: UpdateIotSimType) {
  if (!id || !newInfo || Object.keys(newInfo).length === 0) {
    return { success: false, message: '更新失败，请填入需要更新的信息' };
  }
  const res = await request(`/api/iot-sim/v1.0.0/iotSim/${id}`, {
    method: 'PUT',
    data: newInfo,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data, message: '更新成功' };
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}

export async function updateIotSimFlowInfo(id: string) {
  if (!id) {
    return { success: false, message: '更新失败' };
  }
  const res = await request(`/api/iot-sim/v1.0.0/update/${id}`, {
    method: 'PUT',
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res.data, message: '更新成功' };
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}

export async function getSimInfo(iccid: string, provider: string) {
  if (!iccid || !provider) return { success: false, message: '缺少必要参数' };
  const res = await request('/api/iot-sim/v1.0.0/iotSim/detail', {
    method: 'GET',
    params: {
      iccid,
      provider,
    },
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res?.data, message: '获取成功' };
  return {
    success: false,
    data: res?.data,
    message: res?.message || '获取失败',
  };
}

export async function queryAnyIotSimInfo(iccid: string) {
  if (!iccid) return { success: false, message: '缺少必要参数' };
  const res: ResponseData<AnyIotSimDetail> = await request(
    '/api/iot-sim/v1.0.0/iotSim/open-detail',
    {
      method: 'GET',
      params: {
        iccid,
      },
    },
  );
  if (res?.code === SUCCESS_CODE) return { success: true, data: res?.data, message: '获取成功' };
  return {
    success: false,
    data: res?.data,
    message: res?.message || '获取失败',
  };
}

export async function getSimUsage(iccid: string, provider: string, date?: string) {
  if (!iccid || !provider) return { success: false, message: '缺少必要参数' };
  const res = await request('/api/iot-sim/v1.0.0/iotSim/usageLog', {
    method: 'GET',
    params: {
      iccid,
      provider,
      date,
    },
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res?.data, message: '获取成功' };
  return {
    success: false,
    data: res?.data,
    message: res?.message || '获取失败',
  };
}

export async function downloadTemplateFile() {
  return await request('/api/iot-sim/v1.0.0/template-file', {
    responseType: 'blob',
  });
}

export async function exportIotSim(params: { provider?: string; dateTimeRange?: Date[] }) {
  return await request('/api/iot-sim/v1.0.0/export-iotsim', {
    params,
    responseType: 'blob',
  });
}

export async function getIotSimStatistic() {
  const res = await request('/api/iot-sim/v1.0.0/statistic');
  if (res?.code === SUCCESS_CODE) return { success: true, data: res?.data, message: '获取成功' };
  return {
    success: false,
    data: res?.data,
    message: res?.message || '获取失败',
  };
}

export async function updateMultiIotSim(params: { sims: MultiSimOperInfo[] }) {
  const res = await request('/api/iot-sim/v1.0.0/multi/update', {
    method: 'PUT',
    data: params,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res?.data, message: '更新成功' };
  return {
    success: false,
    data: res?.data,
    message: res?.message || '更新失败',
  };
}
export async function deleteMultiIotSim(params: { sims: MultiSimOperInfo[] }) {
  const res = await request('/api/iot-sim/v1.0.0/multi/delete', {
    method: 'DELETE',
    data: params,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res?.data, message: '删除成功' };
  return {
    success: false,
    data: res?.data,
    message: res?.message || '删除失败',
  };
}

export async function queryDiscardedIotSimList(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: DiscardedIotSimTableListItem[];
}> {
  const res = await request('/api/iot-sim/v1.0.0/discard/list', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function discardMultiIotSim(params: { sims: MultiSimOperInfo[] }) {
  const res = await request('/api/iot-sim/v1.0.0/multi/discard', {
    method: 'PUT',
    data: params,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res?.data, message: '舍弃成功' };
  return {
    success: false,
    data: res?.data,
    message: res?.message || '舍弃失败',
  };
}

export async function deleteDiscardIotSim(id: string) {
  const res = await request(`/api/iot-sim/v1.0.0/discard/${id}`, {
    method: 'DELETE',
  });
  if (res?.code === SUCCESS_CODE) return { success: true, message: '删除成功' };
  return { success: false, message: res?.message || '删除失败' };
}

export async function deleteMultiDiscardIotSim(params: { sims: MultiSimOperInfo[] }) {
  const res = await request('/api/iot-sim/v1.0.0/multi/discard/delete', {
    method: 'DELETE',
    data: params,
  });
  if (res?.code === SUCCESS_CODE) return { success: true, data: res?.data, message: '删除成功' };
  return {
    success: false,
    data: res?.data,
    message: res?.message || '删除失败',
  };
}
